﻿#ifndef APPSETTINGS_H
#define APPSETTINGS_H
 
#include <QMutex>
#include <QSettings>
 
// 配置文件
#define CONFIG_FILE_NAME QString("./cfg.ini")
 
#define JSONPATH QString("JSON_PATH")

#define appSettingsInstance AppSettings::getInstance()
 
class AppSettings
{
public:
    static AppSettings* getInstance()
    {
        if(appSettings == nullptr)
        {
            static QMutex mutex;
            mutex.lock();
            if(appSettings == nullptr)
                appSettings = new AppSettings;//加锁是为了避免线程检查条件后，开始创建对象还未创建时，切换线程，其他线程也继续创建对象
            if(settings == nullptr)
                settings = new QSettings(CONFIG_FILE_NAME,QSettings::IniFormat);
            mutex.unlock();
        }

        return appSettings;
    }

    static QVariant getSetting(QString key)
    {
        return settings->value(key);
    }
 
    static void setSetting(QString key, QVariant value)
    {
        return settings->setValue(key,value);
    }

   // static void quit();
private:
    AppSettings(){}
    ~AppSettings(){}
    AppSettings(const AppSettings&){}//禁止拷贝构造函数
    AppSettings & operator=(const AppSettings &){return *this;}//禁止赋值拷贝构造函数

private:
    static AppSettings *appSettings;
    static QSettings *settings;
};
 
#endif // APPSETTINGS_H
